import QtQuick 2.6
import QtQuick.Layouts 1.2

import "../../Base/UiBase"
import "../../Base/Config"

Rectangle {
    id: root
    height: contentColumn.height

    color: Color.foreground

    property double maxHeight: 631 * Size.ratio
    property alias headSource: head.headSource
    property alias usernameText: head.username
    property alias subject: head.subject
    property alias postTimeText: head.postTimeText
    property alias locationText: head.locationText
    property var images
    property alias commentText: comment.text

    property alias upActivated: foot.upActivated
    property alias submitingComment: foot.upActivated
    property alias upNumber: foot.upNumber

    signal commentPageClicked

    Component.onCompleted: {
        Code.init()
    }

    onImagesChanged: {
        Code.resetImage(images)
    }

    Column {
        id: contentColumn

        spacing: Size.elementInterval

        PostHead {
            id: head
        }

        Column {
            id: mainContent

            width: root.width
            spacing: Size.classicalPost.ident

            Text {
                id: comment


                property double minimumCommentHeight: images.length === 0 ? Size.classicalPost.commentMinimunHeightWithoutImage : Size.classicalPost.commentMinimumHeightWithImage

                function changeHeightByContent() {
                    height = contentHeight + Size.classicalPost.ident
                }

                onTextChanged: if (text.length > Numeral.maxPostTextLength)
                                   text = text.substring(
                                               0, Numeral.maxPostTextLength)

                onMinimumCommentHeightChanged: comment.changeHeightByContent()
                onContentHeightChanged: changeHeightByContent()
                onHeightChanged: height = height < mainContent.minimumCommentHeight ? mainContent.minimumCommentHeight : height

                anchors {
                    left: parent.left
                    right: parent.right
                    leftMargin: Size.classicalPost.ident
                    rightMargin: Size.classicalPost.ident
                }

                font.pixelSize: Size.normalFont

                wrapMode: Text.Wrap
            }
            Row {
                id: imagesRow
                x: Size.classicalPost.ident
                spacing: Size.classicalPost.ident
            }
        }

        PostFoot {
            id: foot
            onCommentPageClicked: root.commentPageClicked()
        }
    }
}
